<template>
	<view class="content">
		<div>
			<u-button type="primary" @click="getUserProfile">获取头像昵称</u-button>
			<!-- <u-button type="primary" @click="toUserLogin">用户密码登录</u-button> -->
		</div>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		univerifyLogin,
		univerifyErrorHandler
	} from '@/common/univerify.js'
	import {
		getDeviceUUID
	} from '@/common/utils.js'

	let weixinAuthService;

	export default {
		data() {
			return {
				platform: uni.getSystemInfoSync().platform,
				canIUseGetUserProfile: false,
			}
		},
		computed: mapState(['forcedLogin', 'hasLogin', 'univerifyErrorMsg', 'hideUniverify']),
		onLoad() {
			// #ifdef APP-PLUS
			plus.oauth.getServices((services) => {
				weixinAuthService = services.find((service) => {
					return service.id === 'weixin'
				})
				if (weixinAuthService) {
					this.hasWeixinAuth = true
				}
			});
			// #endif
			if (uni.getUserProfile) {
				this.canIUseGetUserProfile = true
			}

		},
		methods: {
			...mapMutations(['login']),
			toUserLogin(){
				uni.reLaunch({
					url: '../login/loginH5',
				});
			},
			submit() {
				const db = uniCloud.database();
				let form = {
					nickname: this.form.nickname,
					username: this.form.username,
					sex: this.form.sex,
					phone: this.form.phone,
				}
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						db.collection('uni-id-users').doc(this.form._id).update(form).then(e => {
							uni.navigateBack();
						})
					} else {
						console.log('验证失败');
					}
				});
				
			},
			getUserProfile(e) {
				uni.showLoading({
					title: '加载中...'
				})
				if (this.canIUseGetUserProfile) {
					uni.getUserProfile({
						desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
						success: (res) => {
							console.log(5, res.userInfo.nickName)
							// this.getUserInfo({detail:res})
							var nickName = res.userInfo.nickName
							this.loginByWeixin('weixin', nickName)
						}
					})
				}
			},

			oauth(value) {
				return new Promise((resolve, reject) => {
					// #ifdef APP-PLUS
					weixinAuthService.authorize(function(res) {
						resolve(res.code)
					}, function(err) {
						console.error(err)
						reject(new Error('微信登录失败'))
					});
					// #endif
					// #ifdef MP-WEIXIN
					uni.login({
						provider: 'weixin',
						success(res) {
							resolve(res.code)
						},
						fail(err) {
							console.error('授权登录失败：' + JSON.stringify(err));
							reject(new Error('微信登录失败'))
						}
					})
					// #endif
				})
			},
			toMain(userName) {
				this.login(userName);
				/**
				 * 强制登录时使用reLaunch方式跳转过来
				 * 返回首页也使用reLaunch方式
				 */
				uni.reLaunch({
					url: '../main/main',
				});
			},
			setPwd(uid){
				const db = uniCloud.database();
				db.collection('uni-id-users').doc(uid).update({
					password: "03caebb36670995fc261a275d212cad65e4bbebd",
				}).then(e => {
					// this.savePwd(pwdForm)
				})
			},
			loginByWeixin(value, nickName) {
				this.oauth(value).then((code) => {
					return uniCloud.callFunction({
						name: 'user-center',
						data: {
							action: 'loginByWeixin',
							params: {
								code,
							}
						}
					})
				}).then((res) => {
					uni.hideLoading()
					if (res.result.type == "register") {
						this.setPwd(res.result.uid)
						uni.setStorageSync('uni_id_token', res.result.token)
						uni.setStorageSync('uni_id_token_expired', res.result.tokenExpired)
						uni.setStorageSync('login_type', 'online')
						uni.setStorageSync('username', nickName)
						uni.setStorageSync('nickname', nickName)
						uni.setStorageSync('user_id', res.result.uid)
						// this.form._id = res.result.uid
						// this.form.nickname = nickName
						// this.popShow =true
						uni.navigateTo({
							url: '../user/edit?id=' + res.result.uid,
						});
					} else {
						uni.setStorageSync('uni_id_token', res.result.token)
						uni.setStorageSync('uni_id_token_expired', res.result.tokenExpired)
						uni.setStorageSync('login_type', 'online')
						uni.setStorageSync('username', nickName)
						uni.setStorageSync('nickname', nickName)
						uni.setStorageSync('user_id', res.result.uid)
						this.toMain(nickName)
					}
					// if (res.result.code === 0) {
					// 	console.log(111, res.result)
					// 	uni.setStorageSync('uni_id_token', res.result.token)
					// 	uni.setStorageSync('uni_id_token_expired', res.result.tokenExpired)
					// 	uni.setStorageSync('login_type', 'online')
					// 	uni.setStorageSync('username', nickName)
					// 	this.toMain(nickName)
					// }
				}).catch((e) => {
					console.error(e)
					uni.showModal({
						showCancel: false,
						content: '微信登录失败，请稍后再试'
					})
				})
			},

		},
		onReady() {
			// #ifdef MP-WEIXIN
			this.isDevtools = uni.getSystemInfoSync().platform === 'devtools';
			// #endif
		}
	}
</script>

<style>
	.login-type {
		display: flex;
		justify-content: center;
	}

	.login-type-btn {
		line-height: 30px;
		margin: 0px 15px;
	}

	.login-type-btn.act {
		color: #0FAEFF;
		border-bottom: solid 1px #0FAEFF;
	}

	.send-code-btn {
		width: 120px;
		text-align: center;
		background-color: #0FAEFF;
		color: #FFFFFF;
	}

	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}

	.action-row navigator {
		color: #007aff;
		padding: 0 10px;
	}

	.oauth-row {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-around;
		flex-wrap: wrap;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
	}

	.oauth-image {
		position: relative;
		width: 50px;
		height: 50px;
		border: 1px solid #dddddd;
		border-radius: 50px;
		background-color: #ffffff;
	}

	.oauth-image image {
		width: 30px;
		height: 30px;
		margin: 10px;
	}

	.oauth-image button {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
	}

	.captcha-view {
		line-height: 0;
		justify-content: center;
		align-items: center;
		display: flex;
		position: relative;
		background-color: #f3f3f3;
	}
</style>
